package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Salary;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.SalaryMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.ISalaryService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements ISalaryService{

    @Autowired
    private SalaryMapper mapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    public void deleteByPrimaryKey(Long id) {

    }

    public void insert(Salary record)
    {
        try {
            Long num = record.getIdentifier().getId();

            Employee employee = employeeMapper.selectByPrimaryKey(num);

            record.setIdentifier(employee);

            Date date = new Date();
            SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM");
            String format = sp.format(date);

            Date parse = sp.parse(format);

            record.setMonth(parse);

            record.setDept(employee.getDept());


            record.setBaseSalary(employee.getPosition().getPay());
            record.setCoefficient(new BigDecimal(employee.getPosition().getCoefficient()));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        mapper.insert(record);
    }

    public Salary selectByPrimaryKey(Long id) {
        return null;
    }

    public List<Salary> selectAll() {
        return null;
    }

    public void updateByPrimaryKey(Salary record) {

    }

    public List<Salary> queryByEmpId(Long id) {
        return null;
    }

    public PageResult query(QueryForObject qo) {

        int total = mapper.queryForTotal(qo);
        if (total == 0) {
            return new PageResult();
        }

        List<Salary> rows = mapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    public Salary queryEmpSalaryByEmpId(Long empId) {
        //获得当前日期的时间
        Date date=new Date();
        int year = date.getYear()+1900;
        int month = date.getMonth();
        String name=year+"-0"+month;
        return mapper.queryEmpSalaryByEmpId(name,empId);
    }
}
